package com.gzhryc.shared_device.oem.code.user.services;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.user.dao.UserTransferMoneyQrcodeDao;
import com.gzhryc.shared_device.oem.code.user.dao.db.UserTransferMoneyQrcode;

import java.sql.SQLException;
import java.util.Date;

public class UserTransferMoneyQrcodeService {

    static Logger log = Logger.getLogger(UserTransferMoneyQrcodeService.class);

    public static UserTransferMoneyQrcodeService self() {
        return MultiDBTools.getService(UserTransferMoneyQrcodeService.class, true);
    }

    public static UserTransferMoneyQrcodeService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, UserTransferMoneyQrcodeService.class, true);
    }

    UserTransferMoneyQrcodeDao dao;

    public UserTransferMoneyQrcodeService(String jdbcKey) {
        this.dao = new UserTransferMoneyQrcodeDao(jdbcKey);
    }

    public UserTransferMoneyQrcode getByUserId(Long userId){
        try {
            return dao.getById(userId);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean save(UserTransferMoneyQrcode transferMoneyQrcode){
        UserTransferMoneyQrcode temp = getByUserId(transferMoneyQrcode.getUserId());
        if(temp != null){
            return update(transferMoneyQrcode);
        }else{
            return add(transferMoneyQrcode);
        }
    }

    public boolean add(UserTransferMoneyQrcode transferMoneyQrcode) {
        transferMoneyQrcode.setCreateDate(new Date());
        try {
            return dao.insert(transferMoneyQrcode) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }

    public boolean update(UserTransferMoneyQrcode transferMoneyQrcode) {
        try {
            return dao.update(transferMoneyQrcode) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }

    public boolean deleteByUserId(Long userId) {
        try {
            return dao.deleteById(userId) >= 0;
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }

}
